<?php
namespace app\common\model;
class OrderOuterExchange extends Order
{
	use \traits\ModelTrait;
	
	const TYPE_UNPAID     = 0;
	const TYPE_UNDELIVERY = 1;
	const TYPE_DELIVERYED = 2;
	const TYPE_RECIIVED   = 3;
	//0未支付 1未发货 2已发货 3已收货
	public static $status = [
		0 => '未支付',
		1 => '未发货',
		2 => '已发货',
		3 => '已收货',
	];
	
	/**
	 * 订单列表
	 */
	public static function orderList($where)
	{
		$model = new self();
		$model->alias('mall');
		if ( isset($where['outer_status']) && $where['outer_status'] != '' ) {
		    $model->where(sprintf('mall.status = %d' , $where['outer_status']));
		}
		
		$model->where(sprintf('m.uid = %d' , $where['uid']));

		$orderList = $model->field("mall.goods_id,mall.address_id,mall.nums,mall.logistic_no,mall.status as outer_status,concat(uad.address,uad.details) as address_details,goods.goods_name,m.id as order_id,m.order_no,m.uid,m.out_trade_no,m.orgn_amt,m.disc_amt,m.order_amt,m.integral,m.golds,m.status,m.pay_type,m.pay_status,m.create_time,m.order_type,m.order_outer_id,m.spread_uid,ifnull(m.remark,'-') as remark,u.nickname,u.mobile")
		->join('__ORDER__ m','mall.id = m.order_outer_id AND m.order_type=' . self::ORDER_TYPE_GOLDS)
		->join('__USER__ u' , 'm.uid = u.uid')
		->join('__USER_ADDRESS__ uad' , 'mall.address_id = uad.id' , 'LEFT')
		->join('__GOLDS_GOODS__ goods','mall.goods_id = goods.id')
		->order('m.create_time desc')
		->page($where['page'] ?? 1, $where['limit'] ?? static::LIMIT)
		->select();
		
		if ( !empty($orderList) ) {
			$orderList = $orderList->toArray();
			
			foreach ( $orderList as &$order ) {
				$order['outer_status_display'] = static::$status[$order['outer_status']] ?? '-';
				$order['status_display'] = parent::$status[$order['status']] ?? '-';
				$order['pay_status_display'] = parent::$pay_status[$order['pay_status']] ?? '-';
				$order['pay_type_display'] = parent::$pay_type[$order['pay_type']] ?? '-';
			}
		}
		return $orderList;
	}
}//Class End
